package com.mryang.hds.service.impl;

import com.mryang.hds.configs.AppException;
import com.mryang.hds.entity.PunchRecord;
import com.mryang.hds.entity.Recovered;
import com.mryang.hds.entity.Temp;
import com.mryang.hds.entity.User;
import com.mryang.hds.mapper.DeisolationMapper;
import com.mryang.hds.mapper.PunchRecordMapper;
import com.mryang.hds.mapper.UserMapper;
import com.mryang.hds.service.IDeisolationService;
import com.mryang.hds.utils.DateUtil;
import com.mryang.hds.utils.ResultCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author Genius
 * @version 1.0.0
 * @ClassName DeisolationServiceImpl.java
 * @Description TODO
 * @createTime 2023年04月04日 14:01:00
 */

@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
@Service
public class DeisolationServiceImpl implements IDeisolationService {


    @Resource
    DeisolationMapper deisolationMapper;

    @Resource
    PunchRecordMapper punchRecordMapper;

    @Resource
    UserMapper userMapper;

    @Override
    public void deisolationSub(Integer userId) {
        try {

            String submitTime = DateUtil.dateToString(new Date(), null);

            deisolationMapper.insertRecovered(userId,submitTime);

        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.RECOVERED_FAIL);
        }

    }


    @Transactional
    @Override
    public List<Recovered> getDLUsers(User token) {

        try {
            List<Recovered> dlUsers = deisolationMapper.getDLUsers(token);

            for (Recovered dlUser : dlUsers) {
                // 获取用户的创建时间
                String[] split = dlUser.getUpdateTime().split("-");
                LocalDate givenDate = LocalDate.of(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2]));
                // 计算指定日期到今天的天数
                long daysBetween = ChronoUnit.DAYS.between(givenDate, LocalDate.now());
                // 设置隔离天数
                dlUser.setGeliDays((int)daysBetween);
            }

            return dlUsers;

        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.RECOVERED_FAIL);
        }

    }

    @Transactional
    @Override
    public Temp getHealthInfo(Integer userId) {
        try {

            Temp temp = new Temp();

            // 1、14天内的健康、异常、未打卡天数
            HashMap<String, Integer> map = new HashMap<>();

                // 获取14天前的日期
            LocalDate today = LocalDate.now();
            String date14DaysAgo = today.minusDays(14).toString();
                // 获取指定日期到今天的健康数据情况
            map.put("jiankang",punchRecordMapper.getPRCountByDataException(userId, "正常", date14DaysAgo));
            map.put("yichang",punchRecordMapper.getPRCountByDataException(userId, "异常", date14DaysAgo));
            map.put("weidaka", punchRecordMapper.getPRCountByDataException(userId, "未打卡", date14DaysAgo));

            temp.setHealthStatus(map);
            // 2、获取14天内的打卡详情
            List<PunchRecord> prCountByDataExceptionDatas = punchRecordMapper.getPRCountByDataExceptionDatas(userId, null, date14DaysAgo);
            temp.setPRList(prCountByDataExceptionDatas);

            return temp;

        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.RECOVERED_FAIL);
        }

    }

    @Override
    public List<Recovered> searchDIS(String searchFlag, String input, User token) {
        try {

            return deisolationMapper.getSearchDISUsers(searchFlag,input,token);

        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.RECOVERED_FAIL);
        }
    }

    @Override
    public void checkDTUser(Integer recoveredId, Boolean checked,Integer userId, Integer auditId) {
        try {

            String auditTime = DateUtil.dateToString(new Date(), null);

            deisolationMapper.checkDTUser(recoveredId,checked,auditId,auditTime);

            if(checked){
                userMapper.editUserRecuperate(1,userId);
            }else {
                userMapper.editUserRecuperate(-1,userId);
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.RECOVERED_FAIL);
        }
    }
}
